<?php
namespace App\Controller;

class AdminAcosController extends AppController{

    public function initialize()
    {
        parent::initialize();
        $this->loadModel('Acos');
    }

    /*
     * create by phpstorm
     * author：Ming
     * date：2018.06.27
     * ps：获取权限列表
     * */
    public function index()
    {
        $acos = $this->AdminAcos->find()
            ->where(["status !=" => "disabled"])
            ->select(["id","parent_id","alias","icon","link"])
            ->order(['lft' => 'asc'])
            ->toArray();
        $this->G->success("Success",$acos);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.11.17
   * ps：批量新增权限
   * */
    public function importAcos()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $acos = $this->request->getData("acos");
        foreach ($acos as $k=>$v){
            $aco = $this->Acos->newEntity();
            $aco->alias = $v['alias'];
            $aco->parent_id = 0;
            $aco->enabled = 1;
            $aco_save = $this->Acos->save($aco);
            if(!empty($v['children'])){
                foreach ($v['children'] as $key=>$value){
                    $aco = $this->Acos->newEntity();
                    $aco->alias = $value['alias'];
                    $aco->parent_id = $aco_save->id;
                    $aco->enabled = 1;
                    $this->Acos->save($aco);
                }
            }
        }
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.01
   * ps：新增权限
   * */
    public function add()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $parent_id = $this->request->getData("parent_id");
        $alias = $this->request->getData("alias");
        $aco = $this->Acos->newEntity();
        $aco->alias = $alias;
        $aco->parent_id = $parent_id;
        $aco->enabled = 1;
        $this->Acos->save($aco);
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.02
   * ps：删除权限
   * */
    public function delete()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData("id");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        $this->Acos->deleteAll(['id'=>$id]);
        $this->G->success('Success');
    }

}
?>